package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.common.util.Jsons;
import com.atguigu.gmall.product.dto.ValueSkuJsonDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.mapper.SpuSaleAttrMapper;
import com.sun.javafx.collections.MappingChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ltystart
 * @description 针对表【spu_sale_attr(spu销售属性)】的数据库操作Service实现
 * @createDate 2025-08-15 14:26:51
 */
@Service
public class SpuSaleAttrServiceImpl extends ServiceImpl<SpuSaleAttrMapper, SpuSaleAttr>
        implements SpuSaleAttrService {
    @Autowired
    private SpuSaleAttrMapper spuSaleAttrMapper;

    /**
     * 查询指定spuId的spu销售属性列表
     *
     * @param spuId
     * @return
     */
    @Override
    public List<SpuSaleAttr> getSpuSaleAttrList(Long spuId) {
        List<SpuSaleAttr> list = spuSaleAttrMapper.getSpuSaleAttrList(spuId);
        return list;
    }

    /**
     * 查询指定sku对应的spu定义的所有销售属性名和值，并标记出当前sku是那一组销售属性，并固定顺序
     *
     * @param skuId
     * @param spuId
     * @return
     */
    @Override
    public List<SpuSaleAttr> getSpuSaleAttrAndValueAndMarkCurrentSku(Long skuId, Long spuId) {
        return spuSaleAttrMapper.getSpuSaleAttrAndValueAndMarkCurrentSku(skuId,spuId);
    }

    /**
     * 根据spu_id，查询这个spu下面的所有sku涉及到的所有销售属性值组合，
     * 返回：Map（"127|128":49，"126|129"：50）的json字符串
     * @param spuId
     * @return
     */
    @Override
    public String getSpuValuesSkuJson(Long spuId) {
       List<ValueSkuJsonDTO> dtos= spuSaleAttrMapper.getSpuValuesSkuJson(spuId);
        Map<String,Long> skuValues = new HashMap<>();
        dtos.stream().forEach(item->{
            skuValues.put(item.getAttrValueConcat(),item.getSkuId());
        });
        String toStr = Jsons.toStr(skuValues);
        return toStr;
    }
}




